"""
给定一个二叉树，找出其最小深度。

最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

说明: 叶子节点是指没有子节点的节点。

示例:

给定二叉树 [3,9,20,null,null,15,7],
    3
   / \
  9  20
    /  \
   15   7
"""


class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def minDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root: return 0
        cur = [root]
        i = 0
        while cur:
            i += 1
            nexts = []
            for x in cur:
                if not x.left and not x.right: return i
                if x.left: nexts.append(x.left)
                if x.right: nexts.append(x.right)
            cur = nexts
        return i